package com.tb;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.tb.util.DateUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.joda.time.YearMonth;
import org.junit.Assert;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.MessageFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Comment generated by idea
 * Created by zhaoYuan on 2017/10/19.
 */
public class CommonTest {

    @Test
    public void test() {
        String rex = "^((13[0-9])|(15[0-9])|(18[0-9])|(14[135789])|(17[0135678]))\\d{8}$";
        Assert.assertTrue("13980694173".matches(rex));
        Assert.assertTrue("15180694173".matches(rex));
        Assert.assertTrue("18080694173".matches(rex));
        Assert.assertTrue("14180694173".matches(rex));
        Assert.assertFalse("14280694173".matches(rex));
        Assert.assertFalse("14480694173".matches(rex));
        Assert.assertFalse("11280694173".matches(rex));
    }

    @org.junit.Test
    public void testDate() {
        Date date = new Date(1505750400000L);
        System.out.println(date);
    }

    @org.junit.Test
    public void testImg() throws IOException {
        File file = new File("d:\\pc_tpl_5.jpg");
        BufferedImage read = ImageIO.read(file);
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] imgData = IOUtils.toByteArray(fileInputStream);
        System.out.println(toHexString(imgData));
    }


    public static String toHexString(byte[] fieldData) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < fieldData.length; i++) {
            int v = (fieldData[i] & 0xFF);
            if (v <= 0xF) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(v));
        }
        return sb.toString();
    }


    @Test
    public void testDateFormat() {
        System.out.println(MessageFormat.format("{0,date,yyyy-MM-dd},{2,number,#},{2,number,0.00},{2,number,0.##}",
                new Date(), 10000, BigDecimal.valueOf(1234.5678), BigDecimal.valueOf(1234.5678), BigDecimal.valueOf(1234.5678)));
    }

    @Test
    public void testSpecialFormat() {
        System.out.println(MessageFormat.format("<img class='init_pic' style='width:640px;' src=''{0}''><div class='for_separator'></div>", "https://img20.360buyimg.com/vc/jfs/t2218/160/1600609450/672652/12ef3837/56600724N28581935.jpg"));
    }


    @Test
    public void testDivide() {
        System.out.println(100000 / 100.0);
    }

    @Test
    public void testJoiner() {
        List<String> emptyList = new LinkedList<String>();
        System.out.println(Joiner.on(",").join(emptyList));
    }

    @Test
    public void testSplitter() {
        String mobiles = "13980694173,13800008888,13245678943\n24234234\t23423432";
        Set<String> mobileSet = new HashSet<String>();
        Pattern pattern = Pattern.compile("\\d{11}");
        Matcher matcher = pattern.matcher(mobiles);
        while (matcher.find()) {
            mobileSet.add(matcher.group());
        }
        System.out.println(mobileSet);
    }

    @Test
    public void removeRepeat() {
        List<String> origins = new LinkedList<>();
        origins.add("aaaa");
        origins.add("aaaa");
        origins.add("bbbb");
        origins.add("cccc");

        origins = removeRepeat(origins);
        Assert.assertEquals(3, origins.size());
    }

    private List<String> removeRepeat(List<String> mobiles) {
        Set<String> temp = new HashSet<>(mobiles);
        return new LinkedList<>(temp);
    }

    @Test
    public void testHex36() {
        String wid = "z12yu97";
        Long lid = Long.valueOf(wid, 36); // 36进制转Long
        System.out.println(System.currentTimeMillis()/1000);
        String sid = Long.toString(System.currentTimeMillis()/1000, 36); // long 转36进制
        System.out.println(lid);
        System.out.println(sid);
    }

    @Test
    public void testHtmlCode() {
        String encodeHtml = "<div class=\"ssd-widget-text W14972529747802I0\">    Koji&nbsp;Hara&nbsp;&#21407;&#24184;&#27835;    </div>";
        System.out.println(StringEscapeUtils.unescapeHtml(encodeHtml));
    }

    @Test
    public void testIdleTime() {
        for (int curHour = 0; curHour < 24; curHour++) {
            System.out.println(curHour + ":" + isIdle(curHour));
        }
    }

    public boolean isIdle(int curHour){
        int start = 22;
        int end = 8;
        boolean twoDay = start > end;
        return twoDay ? !(curHour >= end && curHour < start) : (curHour >= start && curHour < end);
    }

    @Test
    public void testBooleanCompare(){
        System.out.println(Boolean.compare(false, false));
        System.out.println(Boolean.compare(true, false));
        System.out.println(Boolean.compare(false, true));
        System.out.println(Boolean.compare(true, true));
    }

    @Test
    public void testHuoJianDaiLiResult(){
        String content = "123.125.159.122:80 121.196.210.83:3128 120.78.78.141:8888 123.57.76.102:80 122.114.65.20:3128 123.113.148.179:3128 120.79.88.59:3128 180.178.44.146:8088 120.78.62.57:3128 211.159.219.158:80 61.134.29.13:8080 27.40.146.149:61234 113.108.204.74:8888 123.207.25.143:3128 103.228.117.244:8080 112.67.161.206:9797 36.255.134.91:53281 219.245.4.239:3128 219.135.164.245:3128 220.166.242.57:8118 180.118.240.78:61234 121.31.150.9:8123 171.37.173.220:9797 121.31.100.198:8123 180.76.134.106:3128 121.201.69.107:3128 124.206.107.125:3128 1.175.131.112:3128 121.225.84.173:8118 122.228.179.178:80 106.38.159.123:8888 112.27.129.54:3128 1.192.243.127:9999 119.29.180.242:80 111.231.215.145:8080 119.49.144.83:80 39.134.161.13:8080 39.134.161.17:8080 120.77.223.86:3128 101.248.64.68:8080 114.115.140.25:3128 211.159.247.184:80 202.100.83.139:80 111.13.111.184:80 120.77.201.46:8080 121.196.226.246:84 58.240.53.194:80 114.215.102.168:8081 61.155.164.109:3128 116.62.156.137:80 106.75.25.3:80 120.199.64.163:8081 111.2.122.46:8080 117.158.57.2:3128 123.7.38.31:9999 120.114.140.35:3128 171.34.197.71:3128 139.224.132.192:8080 120.79.77.230:8888 123.207.62.235:1080 113.254.114.24:80 124.65.133.94:3128 125.46.69.18:3128 60.250.72.252:8080 139.209.90.50:80 218.106.98.166:53281 120.27.49.85:8090 121.69.45.162:8118 121.31.102.80:8123 211.159.69.202:88 113.254.107.47:3128 113.78.91.4:9797 183.30.197.126:9797 183.30.197.238:9999 119.129.98.63:9797 60.24.145.211:8118 183.51.191.40:9797 27.46.51.171:9797 119.122.212.87:9000 124.152.32.140:53281 115.215.209.190:8118 120.55.176.211:3128 210.6.150.50:80 220.166.13.108:9797 183.13.175.60:9797 61.55.196.231:8000 61.223.69.94:3128 218.247.63.242:8080 110.73.11.94:8123 58.56.149.198:53281 27.46.74.43:9999 218.244.46.6:8118 122.193.117.166:8888 121.232.194.186:9000 125.118.56.184:1080 223.241.117.126:8010 58.251.229.227:9797 180.118.135.187:9000 27.46.74.35:9999 58.251.229.241:9797 219.138.58.93:3128 110.72.22.231:8123 120.8.94.200:9999 27.213.245.133:8888 39.86.236.171:8888 182.121.203.45:9999 211.147.67.150:80 119.28.99.194:3128 202.123.27.88:80 115.29.236.46:3128 218.56.0.234:3128 218.32.94.77:8080 219.136.173.29:9999 219.223.251.173:3128 218.4.199.94:3128 54.223.94.193:3128 117.169.36.98:80 121.43.178.58:3128 166.111.80.162:3128 183.64.111.243:80 61.234.123.16:82 61.155.164.111:3128 116.199.115.79:82 122.224.227.202:3128 116.199.2.209:80 116.62.11.138:3128 58.215.120.223:3128 116.199.2.197:80 218.201.98.196:3128 120.26.14.14:3128 116.199.2.208:80 113.207.44.70:3128 61.4.184.180:3128 39.134.161.18:8080 218.26.217.77:3128 39.134.161.14:8080 211.159.247.232:80 121.199.42.198:3129 58.220.95.107:8080 39.106.97.102:8080 183.146.211.142:8080 219.149.46.151:3129 61.155.164.107:3128 124.238.235.135:8000 58.240.53.196:8080 121.248.112.20:3128 101.200.89.170:8888 211.159.247.216:80 39.134.146.67:8080 183.136.232.149:9999 36.36.201.253:8080 103.1.40.212:3128 183.163.36.220:42421 49.51.41.174:3128 60.214.232.28:9999 54.223.217.191:8888 54.223.83.39:8888 183.184.195.10:9797 113.252.232.249:8080 119.23.136.152:8080 58.255.38.139:9797 118.193.219.165:80 36.228.41.227:8888 114.219.91.25:3128 183.238.123.186:8118 202.98.197.242:3128 223.223.187.195:80 203.222.16.183:80 223.255.191.109:3128 222.208.209.69:8080 183.214.79.102:9999 120.27.210.246:8889 14.28.136.225:8088 59.38.63.31:9797 218.23.129.180:41239 121.69.25.58:8118 121.69.42.90:8118 111.155.116.220:8123 116.6.120.82:808 1.175.130.97:3128 61.185.137.126:3128 59.127.38.117:8080 103.14.26.118:8080 113.68.156.171:9999 180.160.47.142:8123 117.141.155.241:53281 182.92.242.11:80 183.52.150.88:61234 61.155.164.108:3128 118.171.221.10:3128 183.52.150.231:61234 182.61.117.113:80 114.215.192.184:8081 103.224.101.73:8080 118.170.47.119:8888 58.22.61.211:3128 116.52.68.92:9999 112.16.9.198:81 101.132.148.7:8080 113.77.241.52:9797";
//        String content = "定单号[ddbh]不能为空";
//        String content = "123.125.159.122:80 121.196.210.83:3128 ";

        System.out.println(content.matches("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d+)(\\s+(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d+)\\s*)*"));
    }

    @Test
    public void testHandleIP(){
        String ips="101.205.52.72:4374, 106.46.136.74:4325, 106.110.205.212:4356, 1.50.169.41:4320, 111.179.11.119:4384, 106.110.103.28:4364, 111.179.88.155:4384,";

        String pattern = "UPDATE  jd_ip_proxy ip \n" +
                "SET ip.next_run_time=NOW(),ip.error_score=0,ip.test_success=0,ip.error_msg=''''\n" +
                "WHERE ip.ip IN ({0});\n";

        String selectPattern = "\n" +
                " SELECT ip.ip,ip.next_run_time,ip.error_score,ip.last_test_time,ip.last_test_success_time,ip.test_success,ip.error_msg \n" +
                " FROM jd_ip_proxy ip \n WHERE ip.ip IN ({0});\n";

        String ipStr = ips.replaceAll(":\\d+(?=,|$)", "'").replaceAll("^", "'").replaceAll(",$", "").replaceAll(",\\s*",",'");
        System.out.println(MessageFormat.format(pattern,ipStr));
        System.out.println(MessageFormat.format(selectPattern,ipStr));
    }

    @Test
    public void testParseIp(){
        String result="111.179.11.119:4384, 111.179.88.155:4384, 101.205.52.72:4374, 106.110.205.212:4356, 106.110.103.28:4364, 106.46.136.74:4325, 1.50.169.41:4320,";
        Pattern pattern = Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):(\\d+)");
        Matcher matcher = pattern.matcher(result);
        while (matcher.find()){
            System.out.println("ip:" + matcher.group(1) + ", port:" + matcher.group(2));
        }
    }

    @Test
    public void testProcess(){
        System.out.println((int)(((double)15 / 37) * 100));
    }

    @Test
    public void testStringRex(){
        String title = "天堂竹创意家居厨房用品用具小百SN8829货生活小用品家用小东西懒人小玩意神器SN8829";
        title = title.replaceAll("\\w+$","");
        System.out.println(title);
    }

    @Test
    public void testCastNull(){
        Map<String,Object> param = new HashMap<>();
        Map<Long,Long> cateMap = (Map<Long, Long>) param.get("isbn");
        System.out.println(cateMap);
    }

    @Test
    public void testBigDecimalMoney(){
        BigDecimal money = new BigDecimal("100.01$");
        System.out.println(money);
    }

    @Test
    public void testBigDecimal(){
        BigDecimal money = new BigDecimal("100.2351").setScale(2, RoundingMode.HALF_UP);
        System.out.println(money);
    }

    @Test
    public void testJSON(){
        String json = "{\"num\":100,\"giveNum\":0}";
        String s = JSON.toJSONString(json);
        JSONObject jsonObject = JSON.parseObject(s);
        System.out.println(jsonObject.getIntValue("num"));
    }

    @Test
    public void testIpMatch(){
        boolean match = IpWildcardMatcher.match("192.168.*.*", "192.168.3.4");
        Assert.assertTrue(match);
        match = IpWildcardMatcher.match("192.168.3.?", "192.168.3.4");
        Assert.assertTrue(match);
        match = IpWildcardMatcher.match("192.168.3.?", "192.168.3.11");
        Assert.assertFalse(match);
        match = IpWildcardMatcher.match("192.16?.3.10", "192.168.3.11");
        Assert.assertFalse(match);
        match = IpWildcardMatcher.match("192.16*", "192.168.3.11");
        Assert.assertFalse(match);
        match = IpWildcardMatcher.match("192.168.*", "192.168.3.4");
        Assert.assertTrue(match);
    }

    @Test
    public void testJodaTime(){
        Date date = YearMonth.now().toLocalDate(1).toDate();
        System.out.println(MessageFormat.format("{0,date,yyyy-MM-dd HH:mm:ss}",date));
    }

    @Test
    public void testRandom(){
        Map<Boolean,Integer> count = new LinkedHashMap<>();
        count.put(true,0);
        count.put(false,0);
        for(int i = 0; i<10000; i++){
            boolean v = isUseLingLongByRandom();
            count.put(v,count.get(v) + 1);
        }
        System.out.println(JSON.toJSONString(count));

    }

    private boolean isUseLingLongByRandom() {
        final int rate = 10; //使用玲珑的比例
        Random random = new Random();
        int roll = random.nextInt(100);
        System.out.println(roll);
        return roll < rate;
    }

    @Test
    public void testRegex(){
        String downloadServer = "job.huoyuan88.cn/item/export/download";
        downloadServer = downloadServer.replaceAll("^((http(s)?:)?//)?","//");
        System.out.println(downloadServer);

        downloadServer = "//job.huoyuan88.cn/item/export/download";
        downloadServer = downloadServer.replaceAll("^((http(s)?:)?//)?","//");
        System.out.println(downloadServer);

        downloadServer = "http://job.huoyuan88.cn/item/export/download";
        downloadServer = downloadServer.replaceAll("^((http(s)?:)?//)?","//");
        System.out.println(downloadServer);

        downloadServer = "https://job.huoyuan88.cn/item/export/download";
        downloadServer = downloadServer.replaceAll("^((http(s)?:)?//)?","//");
        System.out.println(downloadServer);

        String remark = "[TB#241127629202371925 \n ￥19.40]sdfsdf\nsdfdsf";
        System.out.println(remark.matches("(?s).*\\[.*].*"));
    }

    @Test
    public void testPartReplace(){
        String remark = "测试软件\nsss [TB#241127629202371925 ￥19.40] bbb\nddd";
        String newRemark = "[TB#241127629202371925 ￥19.40]";
        String pattern = "(?s)\\[.*]";
        System.out.println(remark.matches(pattern));
        System.out.println(remark.replaceAll(pattern,newRemark));
    }

    /**
     * 正则：正向预查 split。
     */
    @Test
    public void testSplit(){
        String origin = "1627207:3232483:颜色分类:温馨提示;以下是指表带的宽度，不是长度的哦;148242406:3269253:尺寸:18mm";
//        origin = origin.replaceAll(";(?=\\d+:)","\n");
//        System.out.println(origin);
        String[] split = origin.split(";(?=\\d+:)");
        System.out.println(split);
    }

    @Test
    public void testMatch(){
        String city = "城口县";
        System.out.println(city.matches(".*县$"));

        String emoji = "\\xF0\\x9F\\x90\\xB1";
        System.out.println(emoji.replaceAll("\\\\x", ""));
    }

    @Test
    public void testMapNullKey(){
        Map<String,String> map = new HashMap<>();
        map.put(null,"aa");
        map.put("k1","bb");
        System.out.println(JSON.toJSONString(map));
    }

    @Test
    public void testaaa(){
        String name = "圆通123速递123123(YTO)(95554)";
        name = name.replaceAll("\\([^)]*\\)*", "");
        System.out.println(name);
        name = name.replaceAll("\\d+", "");
        System.out.println(name);
    }

    @Test
    public void testInsertString(){
        String url = "https://image.suning.cn/uimg/sop/commodity/189591123720652765437586.JPG";
        String reg = "\\.([^.]+)$";
        String s = url.replaceAll(reg, "._x$1"); //$+数字 可以引用到捕获组的值
        System.out.println(s);

        String reg2 = "\\.(?=([^.]+$))"; //利用正向预查，只替换点， 预查部分，只参与匹配， 不会被替换
        System.out.println(url.replaceAll(reg2,"._x"));

        String reg3 = "(?<=(\\.))[^.]+$"; //利用反向预查，替换jpg，不替换点，点只参与匹配， 不会被替换
        System.out.println(url.replaceAll(reg3,"_x$0"));
    }


}
